import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

def randomPlot():
    '''
    构造随机数矩阵来绘制热力图
    '''
    data = np.random.rand(8, 8)
    key_list = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
    pd_data = pd.DataFrame(np.round(data, 4), columns=key_list, index=key_list)
    print('data:\n', data)
    mask = np.zeros_like(pd_data)
    mask[np.tril_indices_from(mask)] = True
    print('mask:\n', mask)
    fig, ax = plt.subplots(figsize=(10, 10))
    ax = sns.heatmap(
                pd_data, 
                linewidths=0.1, # 设置每个单元方块的间隔
                
                annot=True, # 是否在颜色格子上显示数值
                
                vmax=1, vmin=0, # 数值范围
                xticklabels=list('ABCDEFGH'), # 是否显示X轴
                yticklabels=True, # 是否显示Y轴
                square=True, 
                
                cmap="YlGnBu" # 指定填充色
    )
    ax.set_title(' Heat Map ', fontsize=18)
    ax.set_ylabel('Y', fontsize=18)
    ax.set_xlabel('X', fontsize=18)
    # plt.savefig('Random.png') # 保存图片
    plt.show()

randomPlot()